package com.dianping.cat.home.router.transform;

import com.dianping.cat.home.router.IEntity;
import com.dianping.cat.home.router.entity.DefaultServer;
import com.dianping.cat.home.router.entity.Domain;
import com.dianping.cat.home.router.entity.Group;
import com.dianping.cat.home.router.entity.GroupServer;
import com.dianping.cat.home.router.entity.Network;
import com.dianping.cat.home.router.entity.NetworkPolicy;
import com.dianping.cat.home.router.entity.RouterConfig;
import com.dianping.cat.home.router.entity.Server;
import com.dianping.cat.home.router.entity.ServerGroup;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;













public class DefaultJsonParser<T>
{
  private DefaultLinker m_linker;
  private Stack<String> m_tags;
  private Stack<Object> m_objs;
  private List<T> m_entities;
  private Class<?> m_entityClass;

  private DefaultJsonParser(Class<?> entityClass) {
/*  47 */     this.m_linker = new DefaultLinker(true);

/*  49 */     this.m_tags = new Stack();

/*  51 */     this.m_objs = new Stack();

/*  53 */     this.m_entities = new ArrayList();




/*  58 */     this.m_entityClass = entityClass;
  }


/*  62 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, InputStream in) throws IOException { return (T)parse(entityClass, new InputStreamReader(in, "utf-8")); }



  public static <T extends IEntity<T>> T parse(Class<T> entityClass, Reader reader) throws IOException {
/*  67 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  69 */     parser.onArrayBegin();
/*  70 */     parser.parse(new JsonReader(reader));
/*  71 */     parser.onArrayEnd();

/*  73 */     if (parser.m_entities.isEmpty()) {
/*  74 */       return null;
    }
/*  76 */     return (T)(IEntity)parser.m_entities.get(0);
  }



/*  81 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, String json) throws IOException { return (T)parse(entityClass, new StringReader(json)); }



/*  85 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, InputStream in) throws Exception { return parseArray(entityClass, new InputStreamReader(in, "utf-8")); }



  public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, Reader reader) throws Exception {
/*  90 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  92 */     parser.parse(new JsonReader(reader));
/*  93 */     return (List<T>) parser.m_entities;
  }


/*  97 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, String json) throws Exception { return parseArray(entityClass, new StringReader(json)); }




  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 103 */     if (value == null || value.length() == 0) {
/* 104 */       return defaultValue;
    }

/* 107 */     if (type == Boolean.class)
/* 108 */       return (T)Boolean.valueOf(value); 
/* 109 */     if (type == Integer.class)
/* 110 */       return (T)Integer.valueOf(value); 
/* 111 */     if (type == Long.class)
/* 112 */       return (T)Long.valueOf(value); 
/* 113 */     if (type == Short.class)
/* 114 */       return (T)Short.valueOf(value); 
/* 115 */     if (type == Float.class)
/* 116 */       return (T)Float.valueOf(value); 
/* 117 */     if (type == Double.class)
/* 118 */       return (T)Double.valueOf(value); 
/* 119 */     if (type == Byte.class)
/* 120 */       return (T)Byte.valueOf(value); 
/* 121 */     if (type == Character.class) {
/* 122 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 124 */     return (T)value;
  }


  private Object createRootEntity() {
    try {
/* 130 */       return this.m_entityClass.newInstance();

    }
/* 133 */     catch (Exception e) {
/* 134 */       throw new RuntimeException(String.format("Unable to create entity(%s) instance!", new Object[] { this.m_entityClass.getName() }), e);
    }
  }


/* 139 */   private boolean isTopLevel() { return (this.m_objs.size() == 1); }


  protected void onArrayBegin() {
/* 143 */     if (this.m_objs.isEmpty()) {
/* 144 */       this.m_objs.push(this.m_entities);
/* 145 */       this.m_tags.push("");
    } else {
/* 147 */       Object parent = this.m_objs.peek();
/* 148 */       String tag = (String)this.m_tags.peek();

/* 150 */       if (parent instanceof Group) {
/* 151 */         if ("servers".equals(tag)) {
/* 152 */           this.m_objs.push(parent);
        } else {
/* 154 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
      } else {
/* 157 */         throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
      }
    }
  }
  protected void onArrayEnd() {
/* 162 */     this.m_objs.pop();
/* 163 */     this.m_tags.pop();
  }


/* 167 */   protected void onName(String name) { this.m_tags.push(name); }


  protected void onObjectBegin() {
/* 171 */     if (isTopLevel()) {
/* 172 */       this.m_objs.push(createRootEntity());
/* 173 */       this.m_tags.push("");
    } else {
/* 175 */       Object parent = this.m_objs.peek();
/* 176 */       String tag = (String)this.m_tags.peek();

/* 178 */       if (parent instanceof RouterConfig) {
/* 179 */         if ("default-servers".equals(tag) || "network-policies".equals(tag) || "server-groups".equals(tag) || "domains".equals(tag)) {
/* 180 */           this.m_objs.push(parent);
        } else {
/* 182 */           String parentTag = (this.m_tags.size() >= 2) ? (String)this.m_tags.get(this.m_tags.size() - 2) : null;

/* 184 */           if ("default-servers".equals(parentTag)) {
/* 185 */             DefaultServer defaultServers = new DefaultServer();

/* 187 */             this.m_linker.onDefaultServer((RouterConfig)parent, defaultServers);
/* 188 */             this.m_objs.push(defaultServers);
/* 189 */           } else if ("network-policies".equals(parentTag)) {
/* 190 */             NetworkPolicy networkPolicies = new NetworkPolicy();

/* 192 */             this.m_linker.onNetworkPolicy((RouterConfig)parent, networkPolicies);
/* 193 */             this.m_objs.push(networkPolicies);
/* 194 */           } else if ("server-groups".equals(parentTag)) {
/* 195 */             ServerGroup serverGroups = new ServerGroup();

/* 197 */             this.m_linker.onServerGroup((RouterConfig)parent, serverGroups);
/* 198 */             this.m_objs.push(serverGroups);
/* 199 */           } else if ("domains".equals(parentTag)) {
/* 200 */             Domain domains = new Domain();

/* 202 */             this.m_linker.onDomain((RouterConfig)parent, domains);
/* 203 */             this.m_objs.push(domains);
          } else {
/* 205 */             throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
          }
        }
/* 208 */       } else if (parent instanceof NetworkPolicy) {
/* 209 */         if ("networks".equals(tag)) {
/* 210 */           this.m_objs.push(parent);
        } else {
/* 212 */           String parentTag = (this.m_tags.size() >= 2) ? (String)this.m_tags.get(this.m_tags.size() - 2) : null;

/* 214 */           if ("networks".equals(parentTag)) {
/* 215 */             Network networks = new Network();

/* 217 */             this.m_linker.onNetwork((NetworkPolicy)parent, networks);
/* 218 */             this.m_objs.push(networks);
          } else {
/* 220 */             throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
          }
        }
/* 223 */       } else if (parent instanceof ServerGroup) {
/* 224 */         if ("group-servers".equals(tag)) {
/* 225 */           this.m_objs.push(parent);
        } else {
/* 227 */           String parentTag = (this.m_tags.size() >= 2) ? (String)this.m_tags.get(this.m_tags.size() - 2) : null;

/* 229 */           if ("group-servers".equals(parentTag)) {
/* 230 */             GroupServer groupServers = new GroupServer();

/* 232 */             this.m_linker.onGroupServer((ServerGroup)parent, groupServers);
/* 233 */             this.m_objs.push(groupServers);
          } else {
/* 235 */             throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
          }
        }
/* 238 */       } else if (parent instanceof Domain) {
/* 239 */         if ("groups".equals(tag)) {
/* 240 */           this.m_objs.push(parent);
        } else {
/* 242 */           String parentTag = (this.m_tags.size() >= 2) ? (String)this.m_tags.get(this.m_tags.size() - 2) : null;

/* 244 */           if ("groups".equals(parentTag)) {
/* 245 */             Group groups = new Group();

/* 247 */             this.m_linker.onGroup((Domain)parent, groups);
/* 248 */             this.m_objs.push(groups);
          } else {
/* 250 */             throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
          }
        }
/* 253 */       } else if (parent instanceof Group) {
/* 254 */         if ("servers".equals(tag)) {
/* 255 */           Server servers = new Server();

/* 257 */           this.m_linker.onServer((Group)parent, servers);
/* 258 */           this.m_objs.push(servers);
/* 259 */           this.m_tags.push("");
        } else {
/* 261 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
      } else {
/* 264 */         throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
      }
    }
  }

  protected void onObjectEnd() {
/* 270 */     this.m_tags.pop();

/* 272 */     Object entity = this.m_objs.pop();

/* 274 */     if (isTopLevel()) {
/* 275 */       this.m_entities.add((T)entity);
    }
  }

  protected void onValue(String value) {
/* 280 */     Object parent = this.m_objs.peek();
/* 281 */     String tag = (String)this.m_tags.pop();

/* 283 */     if (parent instanceof RouterConfig) {
/* 284 */       parseForRouterConfig((RouterConfig)parent, tag, value);
/* 285 */     } else if (parent instanceof DefaultServer) {
/* 286 */       parseForDefaultServer((DefaultServer)parent, tag, value);
/* 287 */     } else if (parent instanceof NetworkPolicy) {
/* 288 */       parseForNetworkPolicy((NetworkPolicy)parent, tag, value);
/* 289 */     } else if (parent instanceof Network) {
/* 290 */       parseForNetwork((Network)parent, tag, value);
/* 291 */     } else if (parent instanceof ServerGroup) {
/* 292 */       parseForServerGroup((ServerGroup)parent, tag, value);
/* 293 */     } else if (parent instanceof GroupServer) {
/* 294 */       parseForGroupServer((GroupServer)parent, tag, value);
/* 295 */     } else if (parent instanceof Domain) {
/* 296 */       parseForDomain((Domain)parent, tag, value);
/* 297 */     } else if (parent instanceof Group) {
/* 298 */       parseForGroup((Group)parent, tag, value);
/* 299 */     } else if (parent instanceof Server) {
/* 300 */       parseForServer((Server)parent, tag, value);
    } else {
/* 302 */       throw new RuntimeException(String.format("Unknown tag(%s) under %s!", new Object[] { tag, parent }));
    }
  }

  private void parse(JsonReader reader) throws IOException {
    try {
/* 308 */       reader.parse(this);
/* 309 */     } catch (EOFException e) {
/* 310 */       if (this.m_objs.size() > 1) {
/* 311 */         throw new EOFException(String.format("Unexpected end while parsing json! tags: %s.", new Object[] { this.m_tags }));
      }
    }

/* 315 */     this.m_linker.finish();
  }

  public void parseForDefaultServer(DefaultServer defaultServer, String tag, String value) {
/* 319 */     if ("id".equals(tag)) {
/* 320 */       defaultServer.setId(value);
/* 321 */     } else if ("weight".equals(tag)) {
/* 322 */       defaultServer.setWeight(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
/* 323 */     } else if ("port".equals(tag)) {
/* 324 */       defaultServer.setPort(((Integer)convert(Integer.class, value, Integer.valueOf(0))).intValue());
/* 325 */     } else if ("enable".equals(tag)) {
/* 326 */       defaultServer.setEnable(((Boolean)convert(Boolean.class, value, Boolean.valueOf(false))).booleanValue());
    } else {
/* 328 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, defaultServer, this.m_tags }));
    }
  }

  public void parseForDomain(Domain domain, String tag, String value) {
/* 333 */     if (!"groups".equals(tag))
    {
/* 335 */       if ("id".equals(tag)) {
/* 336 */         domain.setId(value);
      } else {
/* 338 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, domain, this.m_tags }));
      }  }
  }

  public void parseForGroup(Group group, String tag, String value) {
/* 343 */     if (!"servers".equals(tag))
    {
/* 345 */       if ("id".equals(tag)) {
/* 346 */         group.setId(value);
      } else {
/* 348 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, group, this.m_tags }));
      }  }
  }

  public void parseForGroupServer(GroupServer groupServer, String tag, String value) {
/* 353 */     if ("id".equals(tag)) {
/* 354 */       groupServer.setId(value);
    } else {
/* 356 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, groupServer, this.m_tags }));
    }
  }

  public void parseForNetwork(Network network, String tag, String value) {
/* 361 */     if ("id".equals(tag)) {
/* 362 */       network.setId(value);
    } else {
/* 364 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, network, this.m_tags }));
    }
  }

  public void parseForNetworkPolicy(NetworkPolicy networkPolicy, String tag, String value) {
/* 369 */     if (!"networks".equals(tag))
    {
/* 371 */       if ("id".equals(tag)) {
/* 372 */         networkPolicy.setId(value);
/* 373 */       } else if ("title".equals(tag)) {
/* 374 */         networkPolicy.setTitle(value);
/* 375 */       } else if ("block".equals(tag)) {
/* 376 */         networkPolicy.setBlock(((Boolean)convert(Boolean.class, value, Boolean.valueOf(false))).booleanValue());
/* 377 */       } else if ("server-group".equals(tag)) {
/* 378 */         networkPolicy.setServerGroup(value);
      } else {
/* 380 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, networkPolicy, this.m_tags }));
      }  }
  }

  public void parseForRouterConfig(RouterConfig routerConfig, String tag, String value) {
/* 385 */     if (!"default-servers".equals(tag) && !"network-policies".equals(tag) && !"server-groups".equals(tag) && !"domains".equals(tag))
    {
/* 387 */       if ("backup-server".equals(tag)) {
/* 388 */         routerConfig.setBackupServer(value);
/* 389 */       } else if ("backup-server-port".equals(tag)) {
/* 390 */         routerConfig.setBackupServerPort(((Integer)convert(Integer.class, value, Integer.valueOf(0))).intValue());
/* 391 */       } else if ("startTime".equals(tag)) {
/* 392 */         routerConfig.setStartTime(toDate(value, "yyyy-MM-dd HH:mm:ss"));
/* 393 */       } else if ("domain".equals(tag)) {
/* 394 */         routerConfig.setDomain(value);
/* 395 */       } else if ("endTime".equals(tag)) {
/* 396 */         routerConfig.setEndTime(toDate(value, "yyyy-MM-dd HH:mm:ss"));
      } else {
/* 398 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, routerConfig, this.m_tags }));
      }  }
  }

  public void parseForServer(Server server, String tag, String value) {
/* 403 */     if ("id".equals(tag)) {
/* 404 */       server.setId(value);
/* 405 */     } else if ("port".equals(tag)) {
/* 406 */       server.setPort(((Integer)convert(Integer.class, value, Integer.valueOf(0))).intValue());
/* 407 */     } else if ("weight".equals(tag)) {
/* 408 */       server.setWeight(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
    } else {
/* 410 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, server, this.m_tags }));
    }
  }

  public void parseForServerGroup(ServerGroup serverGroup, String tag, String value) {
/* 415 */     if (!"group-servers".equals(tag))
    {
/* 417 */       if ("id".equals(tag)) {
/* 418 */         serverGroup.setId(value);
/* 419 */       } else if ("title".equals(tag)) {
/* 420 */         serverGroup.setTitle(value);
      } else {
/* 422 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, serverGroup, this.m_tags }));
      }
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 429 */       return (new SimpleDateFormat(format)).parse(str);
/* 430 */     } catch (ParseException e) {
/* 431 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
  static class JsonReader { private Reader m_reader;
    private char[] m_buffer;

    public JsonReader(Reader reader) {
/* 438 */       this.m_buffer = new char[2048];






/* 445 */       this.m_reader = reader;
    }
    private int m_size; private int m_index;
    private char next() throws IOException {
/* 449 */       if (this.m_index >= this.m_size) {
/* 450 */         this.m_size = this.m_reader.read(this.m_buffer);
/* 451 */         this.m_index = 0;

/* 453 */         if (this.m_size == -1) {
/* 454 */           throw new EOFException();
        }
      }

/* 458 */       return this.m_buffer[this.m_index++];
    }

    public void parse(DefaultJsonParser parser) throws IOException {
/* 462 */       StringBuilder sb = new StringBuilder();
/* 463 */       boolean flag = false;

      while (true) {
/* 466 */         char ch = next();

/* 468 */         switch (ch) {
          case '\t':
          case '\n':
          case '\r':
          case ' ':
            continue;
          case '{':
/* 475 */             parser.onObjectBegin();
/* 476 */             flag = false;
            continue;
          case '}':
/* 479 */             if (flag) {
/* 480 */               parser.onValue(sb.toString());
/* 481 */               sb.setLength(0);
            }

/* 484 */             parser.onObjectEnd();
/* 485 */             flag = false;
            continue;
          case '"':
          case '\'':
            while (true) {
/* 490 */               char ch2 = next();

/* 492 */               if (ch2 != ch) {
/* 493 */                 if (ch2 == '\\') {
/* 494 */                   char ch3 = next();

/* 496 */                   switch (ch3) {
                    case 't':
/* 498 */                       sb.append('\t');
                      continue;
                    case 'r':
/* 501 */                       sb.append('\r');
                      continue;
                    case 'n':
/* 504 */                       sb.append('\n');
                      continue;
                  }
/* 507 */                   sb.append(ch3);

                  continue;
                }
/* 511 */                 sb.append(ch2); continue;
              }  break;
            }
/* 514 */             if (!flag) {
/* 515 */               parser.onName(sb.toString());
            } else {
/* 517 */               parser.onValue(sb.toString());
/* 518 */               flag = false;
            }

/* 521 */             sb.setLength(0);
            continue;




          case ':':
/* 528 */             if (sb.length() != 0) {
/* 529 */               parser.onName(sb.toString());
/* 530 */               sb.setLength(0);
            }

/* 533 */             flag = true;
            continue;
          case ',':
/* 536 */             if (sb.length() != 0) {
/* 537 */               if (!flag) {
/* 538 */                 parser.onName(sb.toString());
              } else {
/* 540 */                 parser.onValue(sb.toString());
              }

/* 543 */               sb.setLength(0);
            }

/* 546 */             flag = false;
            continue;
          case '[':
/* 549 */             parser.onArrayBegin();
/* 550 */             flag = false;
            continue;
          case ']':
/* 553 */             parser.onArrayEnd();
/* 554 */             flag = false;
            continue;
        }
/* 557 */         sb.append(ch);
      }
    } }

}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/router/transform/DefaultJsonParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */